package com.japps.turmadochaves.prefs;

import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class HelpersPrefs {

	private static int nextSom = 0;
	private static SharedPreferences prefs;
	private final static String PREFS = "TURMA_DO_CHAVES_PREFS";
	private static ArrayList<String> valuesList = new ArrayList<String>();

	public static void addBooleanPrefs(Context context, String key,
			boolean value) {
		prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
		Editor editor = prefs.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	public static boolean getBooleanPrefs(Context context, String key) {
		prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
		return prefs.getBoolean(key, false);
	}

	private static void loadValues() {
		valuesList = new ArrayList<String>();
		for (String key : prefs.getAll().keySet()) {
			boolean value = prefs.getBoolean(key, false);
			if (value)
				valuesList.add(key);
		}
	}

	public static String getRandomSom() {
		loadValues();
		return valuesList.get(new Random().nextInt(valuesList.size()));
	}

	public static String getNextSom() {
		loadValues();
		nextSom = ((nextSom + 1) % valuesList.size());
		return valuesList.get(nextSom);
	}

	public static boolean isEmpty() {
		loadValues();
		return valuesList.isEmpty();
	}
}
